﻿using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace 选择框组合文本框
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "GET", Tag = "#3c8f52", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "POST", Tag = "#af8123", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "PUT", Tag = "#1156b6", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "PATCH", Tag = "#603695", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "DELETE", Tag = "#8a150f", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "HEAD", Tag = "#308947", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "OPTIONS", Tag = "#a11066", });
            //            this.ComboBoxText.Select.Items.Add(new ComboBoxItem() { Content = "哎呦" });
            this.ComboBoxText.Add("GET", "#3c8f52");
            this.ComboBoxText.Add("POST", "#af8123");
            this.ComboBoxText.Add("PUT", "#1156b6");
            this.ComboBoxText.Add("PATCH", "#603695");
            this.ComboBoxText.Add("DELETE", "#8a150f");
            this.ComboBoxText.Add("HEAD", "#308947");
            this.ComboBoxText.Add("OPTIONS", "#a11066");
            this.ComboBoxText.Select.SelectedIndex = 0;
        }

        private static readonly HttpClient client = new HttpClient();
        private CancellationTokenSource cancellationTokenSource;

        public async Task<string> SendGetRequestAsync(string url, CancellationToken cancellationToken)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(url, cancellationToken);
                response.EnsureSuccessStatusCode(); // 检查响应状态码是否成功
                Debug.WriteLine(response);
                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("请求被取消.");
                return null;
            }
            catch (HttpRequestException e)
            {
                Debug.WriteLine($"请求出错: {e.Message}");
                return null;
            }
        }

        public async void StartRequest(string url)
        {
            // 初始化新的 CancellationTokenSource
            cancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = cancellationTokenSource.Token;
            try
            {
                // 通过新线程进行请求
                string response = await Task.Run(() => SendGetRequestAsync(url, token));

                if (response != null)
                {
                    Debug.WriteLine("请求成功:");
                    Debug.WriteLine(response);
                    await Task.Delay(200);
                    // 假设 Send 是你的控件的属性
                    this.ComboBoxText.Send.IsChecked = false;
                }
                else
                {
                    Debug.WriteLine("请求失败或被取消.");
                    ComboBoxText.Send.IsChecked = false;
                }
            }
            finally
            {
                // 请求完成后清除取消源
                cancellationTokenSource.Dispose();
                cancellationTokenSource = null;
            }
        }
        public void CancelRequest()
        {
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
            }
        }

        private void ComboBoxText_SendClick(object sender, RoutedEventArgs e)
        {
            if (this.ComboBoxText.Send.IsChecked == false)
            {
                CancelRequest();
                Debug.WriteLine("取消");
                this.ComboBoxText.Send.IsChecked = false;
                return;

            }
            Debug.WriteLine("发送");
            this.ComboBoxText.Send.IsChecked = true;
            StartRequest(this.ComboBoxText.Input.Text);

            // Debug.WriteLine("当前索引：" + this.ComboBoxText.GetSelectedIndex);
        }

        private void ComboBoxText_TextChanged(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine(this.ComboBoxText.Input.Text);
        }

        private void ComboBoxText_GetComboBoxIndex(object sender, RoutedEventArgs e)
        {
        }
    }
}
